<?php
/*
Plugin Name: ET Questions
Plugin URI: http://code.google.com/p/et-testimonials/
Description: Manage FAQs on your WordPress site using custom post types and shortcodes.
Version: 0.1.0
Author: Emyr Thomas
License: GPL2
*/

/*  Copyright 2011 Emyr Thomas (email : emyr.thomas@gmail.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 2, as
    published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

add_action( 'init', 'create_et_question_post_type' );
function create_et_question_post_type() {
	 $labels = array(
		'name'              => __( 'FAQ Categories' ),
		'singular_name'     => __( 'FAQ Category' ),
		'search_items'      => __( 'Search FAQ Categories' ),
		'all_items'         => __( 'All FAQ Categories' ),
		'parent_item'       => __( 'Parent FAQ Category' ),
		'parent_item_colon' => __( 'Parent FAQ Category:' ),
		'edit_item'         => __( 'Edit FAQ Category' ), 
		'update_item'       => __( 'Update FAQ Category' ),
		'add_new_item'      => __( 'Add New FAQ Category' ),
		'new_item_name'     => __( 'New FAQ Category Name' ),
        ); 	
  	register_taxonomy( 'et_question_category', array( 'et_question' ), array(
		'hierarchical' => true,
		'labels'       => $labels,
		'show_ui'      => true,
		'query_var'    => true,
		'rewrite'      => array( 'slug' => 'faq-category' ),
        ));
        
	register_post_type( 'et_question',
		array(
			'labels' => array(
				'name' => __( 'FAQs' ),
				'singular_name' => __( 'FAQ' ),
				'edit_item'	=>	__( 'Edit FAQ'),
				'add_new_item'	=>	__( 'Add FAQ')
			),
			'public' => true,
			'show_ui' => true,
			'capability_type' => 'post',
			'rewrite' => array( 'slug' => 'faq', 'with_front' => false ),
			'taxonomies' => array( 'FAQs '),
			'supports' => array('title','editor')	
		)
	);
}

// Change 'Enter title here' text on post screen                                                     
add_action('gettext', 'change_et_question_title_text');                                                          
function change_et_question_title_text( $translation ) {                                                         
    global $post;                                                                                    
    if( isset( $post ) ) {                                                                           
        switch( $post->post_type ) {                                                                 
            case 'et_question' :                                                                  
                if( $translation == 'Enter title here' )                               
                    return 'Enter question here';                                            
            break;                                                                                   
        }                                                                                            
    }                                                                                                
    return $translation;                                                                             
}                                                                                                    
                                                                                                     
// Add filter to ensure the text FAQ is displayed when user updates a question.
if( is_admin() ) add_filter('post_updated_messages', 'et_question_updated_messages');                   
function et_question_updated_messages( $messages ) {                                                    
    global $post, $post_ID;                                                                                
                                                                                                           
        $messages['et_question'] = array(                                                               
        0 => '', // Unused. Messages start at index 1.                                                     
        1 => sprintf( 'FAQ updated. <a href="%s">View FAQ</a>', esc_url( get_permalink( $post->ID ) ) ),
        2 => 'Custom field updated.',                                                                                   
        3 => 'Custom field deleted.',                                                                                   
        4 => 'FAQ updated.',                                                                                    
        /* translators: %s: date and time of the revision */                                                            
        5 => isset( $_GET['revision'] ) ? sprintf( 'FAQ restored to revision from %s', wp_post_revision_title( (int)$_GET['revision'], false ) ) : false,
        6 => sprintf( 'FAQ published. <a href="%s">View FAQ</a>', esc_url( get_permalink( $post->ID ) ) ),                                       
        7 => 'FAQ saved.',                                                                                                                               
        8 => sprintf( 'FAQ submitted. <a target="_blank" href="%s">Preview FAQ</a>', esc_url( add_query_arg( 'preview', 'true', get_permalink($post->ID) ) ) ),
        9 => sprintf( 'FAQ scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview FAQ</a>', date_i18n( 'M j, Y @ G:i', strtotime( $post->post_date ) ), esc_url( get_permalink( $post->ID ) ) ),
        10 => sprintf( 'FAQ draft updated. <a target="_blank" href="%s">Preview FAQ</a>', esc_url( add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ) ) )
    );                                                                                                                                                                             
                                                                                                                                                                                   
    return $messages;                                                                                                                                                              
}

add_shortcode('question', 'et_question_shortcode');
function et_question_shortcode( $atts ) {                                                   
    extract( shortcode_atts( array(                                                            
        'cat'         => '',                                                                   
        'id'          => '',                                                                   
        'title'       => '',                                                                                                                                     
        'numberposts' => -1,                                                                                                                                                      
        'post_status' => 'publish',                                                            
        'orderby'     => 'menu_order ID',                                                                                                                                      
        'order'       => 'ASC'                                                                                                                                                     
    ), $atts ) );                                                                                                                                                                  
                                                                                               
    if( !empty( $id ) ) {                                                                      
        $faqs = get_posts( array(                                                      
        'order'          => $order,                                                            
        'orderby'        => $orderby,                                                                      
        'p'              => $id,                                                               
        'post_type'      => 'et_question',                                                                                          
        'post_status'    => $post_status,                                              
        'numberposts'    => $numberposts) );                                                                                                     
    } else {                                                                                                               
        $faqs = get_posts( array(                            
        'order'          => $order,                                                                                     
        'orderby'        => $orderby,                                         
        'et_question_category'   => $cat,             
        'post_type'      => 'et_question',                  
        'post_status'    => $post_status,                                          
        'numberposts'    => $numberposts) );                                                                                                                     
    }                                                                                                                                                                             

                                                                                                                                       
    if( $faqs ) {                                                              
        if( !empty( $title ) )
            $shortcode .= '<p class="faq-title">'.$title.'</p>';                           

        foreach( $faqs as $faq ) {                                                                                               
            $slug = $faq->post_name;
            $question = $faq->post_title;                                                                          
            $answer = wpautop( $faq->post_content );                               

            if( count( $faqs ) > 1 ) {
                $faq_list .= '<li><a name="q-' . $slug . '" href="#a-' . $slug . '">' . $question . '</a></li>';
                
                $faq_content .= '<dt><a name="a-' . $slug . '">' . $question . '</a></dt>';                          
                $faq_content .= '<dd>' . $answer . '<p><a href="#q-' . $slug . '">Back to questions</a></p></dd>';
            } else {
                $faq_content .= '<dt>' . $question . '</dt>';                          
                $faq_content .= '<dd>' . $answer . '</dd>';
            }
        }                                                                                                  

        if( count( $faqs ) > 1 )
            $shortcode .= '<ul class="faq">' . $faq_list . '</ul>';
        $shortcode .= '<dl class="faq">' . $faq_content . '</dl>';
    }                                                                                          
                                                                                                                                       
    $shortcode = do_shortcode( $shortcode );                           
    return $shortcode;
}

?>
